#include<stdio.h>
#include<stdbool.h>
#include<string.h>
#include<stdlib.h>

#include"transistors.h"
#include"io.h"

/*
 * Name :  Lee
 * Description : Simple transistor description example program
*/


Tran InputTransistor();
void InputString(char *str, int max);
double InputPositiveValue(char *prompt);
int InputTransistorType(char *prompt);
int InputTransistorCaseStyle(char *prompt);
bool HasNextString();

int main()
{
    int i, cnt = 1;
    Tran *trans;
    trans = malloc(sizeof(Tran));
    trans[0] = InputTransistor();

    while(HasNextString())
    {
        cnt++;
        trans = realloc(trans, sizeof(Tran)*cnt);
        trans[cnt-1] = InputTransistor();
    }
    printf("\n");
    for(i=0;i<cnt;i++)
    {
        DisplayTransistor(trans[i]);
        printf("\n");
    }

    printf("transistors!\n");
    free(trans);
    return 0;
}

int InputTransistorType(char *prompt)
{
    char buffer[100];

    while(true)
    {
        printf("%s", prompt);

        fgets(buffer, sizeof(buffer), stdin);
        if(buffer[0] == '\n') continue;

        if(strlen(buffer) > 0)
            buffer[strlen(buffer)-1] = '\0';
        if(strcmp("NPN", buffer) == 0) return NPN;
        if(strcmp("PNP", buffer) == 0) return PNP;
    }
}

int InputTransistorCaseStyle(char *prompt)
{
    char buffer[100];

    while(true)
    {
        printf("%s", prompt);

        fgets(buffer, sizeof(buffer), stdin);
        if(buffer[0] == '\n') continue;

        if(strlen(buffer) > 0)
            buffer[strlen(buffer)-1] = '\0';
        if(strcmp("TO18", buffer) == 0) return TO18;
        if(strcmp("TO92A", buffer) == 0) return TO92A;
        if(strcmp("TO92C", buffer) == 0) return TO92C;
        if(strcmp("TO220", buffer) == 0) return TO220;
        if(strcmp("TO39", buffer) == 0) return TO39;
    }
}

double InputPositiveValue(char *prompt)
{
    char buffer[100];
    double value = 0;
    
    do
    {
        printf("%s", prompt);
 
        /* Get a line of up to 100 characters */
        fgets(buffer, sizeof(buffer), stdin);
    
        /* Remove any \n we may have input */
        if(strlen(buffer) > 0)
            buffer[strlen(buffer)-1] = '\0';
        
        sscanf(buffer, "%lf", &value);
    }
    while(value <= 0);

    return value;
}

bool HasNextString()
{
    char buffer[100];
    char *p;
    do
    {
        printf("Would you like to enter another transistor (Y/N)? ");
        fgets(buffer, sizeof(buffer), stdin);
        if(strlen(buffer) > 0)
            buffer[strlen(buffer)-1] = '\0';
        p = buffer;
        while(*p == ' ')p++;
        if(strlen(p) != 1)continue;
        if(p[0] == 'y' || p[0] == 'Y')return true;
        if(p[0] == 'n' || p[0] == 'N')return false;
    }while(true);
}

void InputString(char *str, int max)
{
    char buffer[100];
    
    /* Get a line of up to 100 characters */
    fgets(buffer, sizeof(buffer), stdin);

    /* Remove any stray newlines from the buffer */
    while (buffer[0] == '\n')
        fgets(buffer, sizeof(buffer), stdin);
    
    /* Remove any \n we may have input */
    if(strlen(buffer) > 0)
        buffer[strlen(buffer)-1] = '\0';
        
    /* Copy up to max characters to our string */
    strncpy(str, buffer, max);
    str[max-1] = '\0';
}

Tran InputTransistor()
{
    Tran t1;
    char buffer[100];
   
    InputString(t1.number, sizeof(t1.number));
    t1.type = InputTransistorType("Input type (NPN or PNP): ");
    t1.caseStyle = TO39;
    t1.caseStyle = InputTransistorCaseStyle("Input case style (in TO18, TO92A, TO92C, TO220, TO39): ");
    t1.pmax = InputPositiveValue("Input pMax: ");
    t1.icmax = InputPositiveValue("Input icMax: ");
    return t1;
}
